package com.application.sistercityapplication.listadapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.application.sistercityapplication.main.R;
import com.application.sistercityapplication.model.AECCityItem;
import com.squareup.picasso.Picasso;

public class CityListShelf extends BaseAdapter{
	private List<AECCityItem> cityList;
	private Context _context;
	ViewHolderListBookMaim holder = null;
	public CityListShelf(List<AECCityItem> magazineList,Context context){
		this.cityList = magazineList;
		this._context = context;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return cityList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return cityList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if(convertView== null){
			holder = new ViewHolderListBookMaim();
			LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = inflater.inflate(R.layout.city_item_layout, null);
			holder.cityImage = (ImageView) convertView.findViewById(R.id.grid_item_cover);
			holder.cityName = (TextView) convertView.findViewById(R.id.city_name);
			
			convertView.setTag(holder);
		}else{
			holder = (ViewHolderListBookMaim) convertView.getTag();
		}
		final AECCityItem mag = cityList.get(position);
		try{
			
			Picasso.with(_context).load(mag.getImageResId()).into(holder.cityImage);
			holder.cityName.setText(mag.getImageName());
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		holder.cityImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(_context, mag.getImageName()+"-"+position, Toast.LENGTH_SHORT).show();
				
			}
		});
		return convertView;
	}
	public class ViewHolderListBookMaim{
		public ImageView cityImage;
		public TextView cityName;
	}

}
